# Contributor: Kaarle Ritvanen <kunkku@alpinelinux.org>
# Maintainer: Kaarle Ritvanen <kunkku@alpinelinux.org>
pkgname=aconf
pkgver=0.8.1
pkgrel=1
pkgdesc="Alpine Configurator"
url="https://gitlab.alpinelinux.org/kunkku/aconf/"
arch="noarch"
license="BSD-2-Clause"
_luaver="5.3"
_lua="lua$_luaver"
depends="$_lua-augeas $_lua-b64 $_lua-cjson $_lua-file-magic $_lua-openrc
	$_lua-ossl $_lua-posix $_lua-stringy uwsgi uwsgi-lua"
makedepends="ldoc"
source="https://dev.alpinelinux.org/archive/aconf/aconf-$pkgver.tar.xz
	$pkgname.initd"
options="!check"


_modules="dnsmasq dns-zone network openssh strongswan"
_escape() {
	echo ${1//-/_}
}

subpackages="$pkgname-doc $pkgname-openrc"
for _m in $_modules; do
	_e=$(_escape $_m)
	subpackages="$subpackages $pkgname-mod-$_m:_$_e"
	eval "_deps_$_e=$_m"
done

_deps_dns_zone=
_deps_network=

package() {
	make LUA_VERSION=$_luaver ROOT_DIR=$pkgdir install
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}


_moddir=usr/share/lua/$_luaver/$pkgname/modules
_augdir=usr/share/$pkgname/augeas

_module() {
	local m=$(_escape $1)
	eval "depends=\"$pkgname \$_deps_$m\""
	mkdir -p "$subpkgdir/$_moddir"
	mv "$pkgdir/$_moddir/$1.lua" "$subpkgdir/$_moddir"

	local lens=$pkgdir/$_augdir/aconf_$m.aug
	[ -f "$lens" ] || return 0
	mkdir -p "$subpkgdir/$_augdir"
	mv "$lens" "$subpkgdir/$_augdir"
}

for _m in $_modules; do
	eval "_$(_escape $_m)() { _module $_m; }"
done

sha512sums="68ae0f5b8c2f5d9c7085f5ebdd8ca6bffd0f83835a77cafdf4b782dcd27d81eec9f5d221e7be48cfaf184aa077ad5e9b0d7389e9eea0cbbf332f4d99330e5bc0  aconf-0.8.1.tar.xz
2706ff356ee8ab72d5b0ac0b6f1bcb3996e7e66457ddc4dbf9745cc6d93a3d9051d141cf0dd28e6b73a422b2d67a0952ed1633471ef7c4bb7393e1ae17e3aa9d  aconf.initd"
